<?php
/**
 * Copyright (c) [2019] [吴跃忠]
 * [selibra] is licensed under the Mulan PSL v1.
 * You can use this software according to the terms and conditions of the Mulan PSL v1.
 * You may obtain a copy of Mulan PSL v1 at:
 * http://license.coscl.org.cn/MulanPSL
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
 * See the Mulan PSL v1 for more details.
 */

namespace Selibra\Http\Interceptor;


use Selibra\Tools\Console;
use Selibra\Di\Annotations\Autowired;

class RequestSpaceInterceptorCollector
{

    /**
     * @var RequestSpaceTree[]
     */
    protected static array $requestSpaceTree;


    /**
     * @param string $name
     * @param $class
     * @param $method
     */
    public static function register(string $name, $class, $method, string $requestMethod = 'ANY')
    {
        $requestSpaceArray = [];
        if ($name !== '/') {
            $requestSpaceArray = array_filter(explode('/', $name));
        }
        if (!in_array('*', $requestSpaceArray)) {
            $requestSpaceArray[] = '/';
        }
        // 添加节点
        $requestSpaceNode = new RequestSpaceNode(reset($requestSpaceArray));
        $requestSpaceNode->setNamespace($class);
        $requestSpaceNode->setMethod($method);
        self::getRequestSpaceTree($requestMethod)->addNode($requestSpaceArray, $requestSpaceNode);
    }


    /**
     * @return RequestSpaceTree
     */
    public static function getRequestSpaceTree($requestMethod)
    {
        $requestMethod = strtoupper($requestMethod);
        if (empty(self::$requestSpaceTree[$requestMethod])) {
            self::$requestSpaceTree[$requestMethod] = new RequestSpaceTree();
        }
        return self::$requestSpaceTree[$requestMethod];
    }


    /**
     * @param string $action
     * @return array
     */
    public static function getInterceptors(string $action, string $requestMethod = 'ANY')
    {
        $requestSpaceArray = array_filter(explode('/', $action));
        // 查找
        $i = 0;
        $interceptors = [];
        $parentrequestSpace = $requestSpaceArray;
        array_pop($parentrequestSpace);
        foreach ($parentrequestSpace as $item) {
            $requestSpace = array_slice($parentrequestSpace, 0, $i + 1);
            $requestSpace[] = '*';
            $nodes = self::getRequestSpaceTree($requestMethod)->getNodes($requestSpace);
            if (!empty($nodes)) {
                $interceptors = array_merge($interceptors, $nodes);
            }
            $i++;
        }
        $requestSpaceArray[] = '/';
        $rootNodes = self::getRequestSpaceTree($requestMethod)->getNodes($requestSpaceArray);
        $interceptors = array_merge($interceptors, $rootNodes);

        $rootNodes = self::getRequestSpaceTree($requestMethod)->getNodes(['*']);
        $interceptors = array_merge($interceptors, $rootNodes ?: []);
        return $interceptors;
    }

}